package io.renren.modules.report.service.impl;

import io.renren.modules.report.entity.*;
import io.renren.modules.report.service.PackageUserService;
import io.renren.modules.report.vo.PackageUserVO;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.report.dao.BusinessPackageDao;
import io.renren.modules.report.service.BusinessPackageService;
import org.springframework.transaction.annotation.Transactional;


@Service("businessPackageService")
public class BusinessPackageServiceImpl extends ServiceImpl<BusinessPackageDao, BusinessPackageEntity> implements BusinessPackageService {

    @Autowired
    PackageUserService packageUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String connectionId = (String)params.get("connectionId");
        String createUserId = (String)params.get("createrId");
        String name = (String)params.get("name");

        IPage<BusinessPackageEntity> page = this.page(
                new Query<BusinessPackageEntity>().getPage(params),
                new QueryWrapper<BusinessPackageEntity>()
                .like(StringUtils.isNotBlank(name),"name", name)
                .eq(createUserId != null,"creater_id", createUserId)
                .eq(StringUtils.isNotBlank(connectionId),"connection_id", connectionId)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveCascade(PackageUserVO packageUserVO) {

        BusinessPackageEntity businessPackageEntity = new BusinessPackageEntity();
        BeanUtils.copyProperties(packageUserVO,businessPackageEntity);

        businessPackageEntity.setCreateTime(new Date());

        this.save(businessPackageEntity);

        packageUserVO.getUserIds().stream().forEach(item->{
            PackageUserEntity packageUserEntity = new PackageUserEntity();
            packageUserEntity.setPackageId(businessPackageEntity.getId());
            packageUserEntity.setUserId(item);
            packageUserService.save(packageUserEntity);
        });

    }

    @Override
    @Transactional
    public void updateCascade(PackageUserVO packageUserVO) {
        BusinessPackageEntity businessPackageEntity = new BusinessPackageEntity();

        BeanUtils.copyProperties(packageUserVO,businessPackageEntity);

        businessPackageEntity.setCreateTime(new Date());
        this.updateById(businessPackageEntity);

        packageUserService.remove(new QueryWrapper<PackageUserEntity>().eq("package_id",businessPackageEntity.getId()));
        packageUserVO.getUserIds().stream().forEach(item->{
            PackageUserEntity packageUserEntity = new PackageUserEntity();
            packageUserEntity.setPackageId(businessPackageEntity.getId());
            packageUserEntity.setUserId(item);
            packageUserService.save(packageUserEntity);
        });
    }

    @Override
    public void deleteCascade(Long[] ids) {
        List<Long> idsList = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(idsList);

        idsList.stream().forEach(item->{
            packageUserService.remove(new QueryWrapper<PackageUserEntity>().eq("package_id",item));
        });

    }

}